home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13892 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.tu-chemnitz.de!news
  2. From: hfst@hrz.tu-chemnitz.de (Hans Steffani)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Beginner need help??????????????
  5. Date: 10 Apr 96 15:38:18 GMT
  6. Organization: University of Technology Chemnitz, FRG
  7. Message-ID: <4kgkvj$qem@narses.hrz.tu-chemnitz.de>
  8. References: <4kem82$5j3@dewey.csun.edu>
  9. NNTP-Posting-Host: click.hrz.tu-chemnitz.de
  10.  
  11. kc44097@csun.edu (chen) writes:
  12.  
  13.  
  14.  
  15. >-------- program 1 -------------------------
  16. >#include <stdio.h>
  17.  
  18. >float answer;  
  19.  
  20. >  answer = 1 / 3;
  21. The caluclation is done in an integral as both operands are 
  22. of integral type. The result is 0 and casted to 0.0 which
  23. means 0.
  24.  
  25. >  printf("The value of 1/3 is %f\n",
  26. >                 answer);
  27.  
  28.  
  29.  
  30.  
  31. >float result;
  32.  
  33. >main()
  34. >{
  35. >  result = 7.0 / 22.0;
  36.  
  37. >  printf("The result is %d\n", result);
  38.  
  39. The type of result is float, not integer. The printf() treats
  40. the bitpattern of result as an integer leads printf() to assume
  41. that the integer 0 is passed. If you do not like this, use
  42.  
  43.    printf("blah %f\n", result );
  44.  
  45. h.f.s.
  46.  
  47. >int   integer;
  48. >float floating;
  49. >  floating = 7.0 / 22.0;
  50. >  
  51. >  integer = floating;
  52.  
  53. >  printf("The value of integer is %f\n", integer);
  54. >???? The answer should be 0.31818 but compiler give me 0.000000 ?????
  55.  
  56. Integer cannot hold 0.31 as it is integral type. Further on
  57. %f does not fit to the type of the argument integer. Change
  58. to 
  59.    printf("The value of integer is %d\n", integer);
  60.  
  61. h.f.s.
  62.  
  63. PS:
  64. Looks like a homework problem but now it's too late.
  65. h.f.s.
  66. --
  67. Hans Friedrich Steffani
  68. Institut fuer Elektrische Maschinen und Antriebe
  69. TU Chemnitz-Zwickau
  70. e-mail: hans.steffani@e-technik.tu-chemnitz.de
  71.